package com.elm.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.elm.dao.SellerOrederDao;
import com.elm.daoimpl.SellerOrderDaoImp;
import com.elm.model.Food;
import com.elm.model.Seller;
import com.elm.model.SellerOrder;
import com.elm.model.ShoppingCar;
import com.elm.model.UserOrder;
import com.elm.service.FoodService;
import com.elm.service.SellerOrderService;
import com.elm.service.ShoppingCarService;
import com.elm.service.UserOrderService;
import com.elm.serviceimpl.FoodServiceImpl;
import com.elm.serviceimpl.SellerOrderServiceImp;
import com.elm.serviceimpl.ShoppingCarServiceImpl;
import com.elm.serviceimpl.UserOrderServiceImpl;

/**
 * 
 * @author change by pgm
 *
 */
public class SelectSellerOrderIdController extends HttpServlet{
SellerOrderService sellerorderservice=new SellerOrderServiceImp();
UserOrderService userorderderservice = new UserOrderServiceImpl();
FoodService fservice = new FoodServiceImpl();
ShoppingCarService scservice = new ShoppingCarServiceImpl();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	// TODO Auto-generated method stub
	doPost(req, resp);
}
@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
	req.setCharacterEncoding("utf8");
	resp.setCharacterEncoding("utf8");
	
	HttpSession session=req.getSession();
	Seller seller=(Seller) session.getAttribute("currentSeller");
	//得到某一商家下的对应状态的订单
	List<SellerOrder> sorders0 = new ArrayList<SellerOrder>();
	List<String> uorderscarid0 = new ArrayList<String>();//过往订单中的carid集合
	List<List<Integer>> intuordercaridss0 = new ArrayList<List<Integer>>();
	List<Integer> foodids0 = new ArrayList<Integer>();//过往订单的foodid
	List<Food> foods0 = new ArrayList<Food>();//过往订单的food
	List<List<Food>> foodss0 = new ArrayList<List<Food>>();
	List<Integer> scar0 = new ArrayList<Integer>();//过往订单的numfood
	List<List<Integer>> scars0 = new ArrayList<List<Integer>>();
	List<Double> orderprice0 = new ArrayList<Double>();
	
	List<SellerOrder> sorders1 = new ArrayList<SellerOrder>();
	List<String> uorderscarid1 = new ArrayList<String>();//已接单订单中的carid集合
	List<List<Integer>> intuordercaridss1 = new ArrayList<List<Integer>>();
	List<Integer> foodids1 = new ArrayList<Integer>();//已接单订单的foodid
	List<Food> foods1 = new ArrayList<Food>();//已接单订单的food
	List<List<Food>> foodss1 = new ArrayList<List<Food>>();
	List<Integer> scar1 = new ArrayList<>();//已接单订单的numfood
	List<List<Integer>> scars1 = new ArrayList<List<Integer>>();
	List<Double> orderprice1 = new ArrayList<Double>();
	
	List<SellerOrder> sorders2 = new ArrayList<SellerOrder>();
	List<String> uorderscarid2 = new ArrayList<String>();//未接单订单中的carid集合
	List<List<Integer>> intuordercaridss2 = new ArrayList<List<Integer>>();
	List<Integer> foodids2 = new ArrayList<Integer>();//未接单订单的foodid
	List<Food> foods2 = new ArrayList<Food>();//未接单订单的food
	List<List<Food>> foodss2 = new ArrayList<List<Food>>();
	List<Integer> scar2 = new ArrayList<>();//未接单订单numfood
	List<List<Integer>> scars2 = new ArrayList<List<Integer>>();
	List<Double> orderprice2 = new ArrayList<Double>();
	
	int sellerid=seller.getSellerid();
	sorders0 = sellerorderservice.getAllSellerOrders(sellerid, 0);//过往订单
	sorders1 = sellerorderservice.getAllSellerOrders(sellerid, 1);//已接单订单
	sorders2 = sellerorderservice.getAllSellerOrders(sellerid, 2);//未接单订单
	session.setAttribute("sorders0", sorders0);
	session.setAttribute("sorders1", sorders1);
	session.setAttribute("sorders2", sorders2);
	//过往订单
	for (SellerOrder sorder0:sorders0) {
		String carid0 = userorderderservice.getCaridByUserOrderId(sorder0.getUserorderid());
		String[] s = carid0.split(",");
		List<Integer> intuordercarids0 = new ArrayList<Integer>();//过往订单的carid int类型集合
		for (int i = 0; i < s.length; i++) {
			intuordercarids0.add(Integer.parseInt(s[i]));
		}
		intuordercaridss0.add(intuordercarids0);
	}
	
	for (int i = 0; i < intuordercaridss0.size(); i++) {
		foodids0 = scservice.getShoppingCarFoodidsByCarids(intuordercaridss0.get(i));
		scar0 = scservice.getShoppingCarnumfoodByCarids(intuordercaridss0.get(i));
		foods0 = fservice.getFoodbyFoodids(foodids0);
		foodss0.add(foods0);
		scars0.add(scar0);
	}
	//计算总价
	for (int i = 0; i < foodss0.size(); i++) {
		double foodprice = 0;//存放每个订单的总价
		for(int j = 0; j < foodss0.get(i).size(); j++) {
			int currentnumfood = scars0.get(i).get(j);
			foodprice = foodprice + Double.parseDouble(foods0.get(j).getFoodprice())*currentnumfood;
		}
		orderprice0.add(foodprice);
	}
	//已接单订单
	for (SellerOrder sorder1:sorders1) {
		String carid1 = userorderderservice.getCaridByUserOrderId(sorder1.getUserorderid());
		String[] s = carid1.split(",");
		List<Integer> intuordercarids1 = new ArrayList<Integer>();//已接单订单的carid int类型集合
		for (int i = 0; i < s.length; i++) {
			intuordercarids1.add(Integer.parseInt(s[i]));
		}
		intuordercaridss1.add(intuordercarids1);
	}
	for (int i = 0; i < intuordercaridss1.size(); i++) {
		foodids1 = scservice.getShoppingCarFoodidsByCarids(intuordercaridss1.get(i));
		scar1 = scservice.getShoppingCarnumfoodByCarids(intuordercaridss1.get(i));
		foods1 = fservice.getFoodbyFoodids(foodids1);
		foodss1.add(foods1);
		scars1.add(scar1);
	}
	//计算总价
		for (int i = 0; i < foodss1.size(); i++) {
			double foodprice = 0;//存放每个订单的总价
			for(int j = 0; j < foodss1.get(i).size(); j++) {
				int currentnumfood = scars1.get(i).get(j);
				foodprice = foodprice + Double.parseDouble(foods1.get(j).getFoodprice())*currentnumfood;
			}
			orderprice1.add(foodprice);
		}
	//未接单订单
	for (SellerOrder sorder2:sorders2) {
		String carid2 = userorderderservice.getCaridByUserOrderId(sorder2.getUserorderid());
		String[] s = carid2.split(",");
		List<Integer> intuordercarids2 = new ArrayList<Integer>();//未接单订单的carid int类型集合
		for (int i = 0; i < s.length; i++) {
			intuordercarids2.add(Integer.parseInt(s[i]));
		}
		intuordercaridss2.add(intuordercarids2);
	}
	for (int i = 0; i < intuordercaridss2.size(); i++) {
		foodids2 = scservice.getShoppingCarFoodidsByCarids(intuordercaridss2.get(i));
		scar2 = scservice.getShoppingCarnumfoodByCarids(intuordercaridss2.get(i));
		foods2 = fservice.getFoodbyFoodids(foodids2);
		foodss2.add(foods2);
		scars2.add(scar2);
	}
	System.out.println(scars2.size());
	//计算总价
		for (int i = 0; i < foodss2.size(); i++) {
			double foodprice = 0;//存放每个订单的总价
			for(int j = 0; j < foodss2.get(i).size(); j++) {
				int currentnumfood = scars2.get(i).get(j);
				foodprice = foodprice + Double.parseDouble(foods2.get(j).getFoodprice())*currentnumfood;
			}
			orderprice2.add(foodprice);
		}
	session.setAttribute("scars0", scars0);
	session.setAttribute("scars1", scars1);
	session.setAttribute("scars2", scars2);
	session.setAttribute("orderprice0", orderprice0);
	session.setAttribute("orderprice1", orderprice1);
	session.setAttribute("orderprice2", orderprice2);
	session.setAttribute("foodss0", foodss0);
	session.setAttribute("foodss1", foodss1);
	session.setAttribute("foodss2", foodss2);
	}
}
